using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The CurrencyCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about CurrencyCodeTable objects.
/// </summary>
public class CurrencyCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of CurrencyCodeTable from the underlying datasource.
/// </summary>
/// <param name="currencyCode">The unique CurrencyCode of the CurrencyCodeTable in the database.</param>
/// <returns>An CurrencyCodeTable when the CurrencyCode was found in the database, or null otherwise.</returns>
public static CurrencyCodeTable GetItem(Int16 currencyCode)
{
CurrencyCodeTable myCurrencyCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@currencyCode", currencyCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myCurrencyCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myCurrencyCodeTable;
}
}

/// <summary>
/// Returns a list with CurrencyCodeTable objects.
/// </summary>
/// <returns>A generics List with the CurrencyCodeTable objects.</returns>
public static CurrencyCodeTableList GetList()
{
CurrencyCodeTableList tempList = new CurrencyCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a CurrencyCodeTable in the database.
/// </summary>
/// <param name="myCurrencyCodeTable">The CurrencyCodeTable instance to save.</param>
/// <returns>The new CurrencyCode if the CurrencyCodeTable is new in the database or the existing CurrencyCode when an item was updated.</returns>
public static Int16 Save(CurrencyCodeTable myCurrencyCodeTable)
{
    Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myCurrencyCodeTable.CurrencyCodeKey == -1)
{myCommand.Parameters.AddWithValue("@currencyCodeKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@currencyCodeKey", myCurrencyCodeTable.CurrencyCodeKey);
}

if (myCurrencyCodeTable.Description == null)
{
    myCommand.Parameters.AddWithValue("@CurrencyCode", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@CurrencyCode", myCurrencyCodeTable.CurrencyCode);
}
if (myCurrencyCodeTable.Description == null)
{
    myCommand.Parameters.AddWithValue("@CurrencySymbol", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@CurrencySymbol", myCurrencyCodeTable.CurrencySymbol);
}

if (myCurrencyCodeTable.Description == null)
{
    myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@description", myCurrencyCodeTable.Description);
}

if (myCurrencyCodeTable.Description == null){
myCommand.Parameters.AddWithValue("@rate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@rate", myCurrencyCodeTable.Rate);
}
if (myCurrencyCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myCurrencyCodeTable.Status);
}
if (myCurrencyCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myCurrencyCodeTable.CreatedDateTime);
}
if (myCurrencyCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myCurrencyCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a CurrencyCodeTable from the database.
/// </summary>
/// <param name="currencyCodeKey">The CurrencyCode of the CurrencyCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(Int16 currencyCodeKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@currencyCodeKey", currencyCodeKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the CurrencyCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static CurrencyCodeTable FillDataRecord(IDataRecord myDataRecord )
{
CurrencyCodeTable myCurrencyCodeTable = new CurrencyCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencyCodeKey")))
{
myCurrencyCodeTable.CurrencyCodeKey = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CurrencyCodeKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencyCode")))
{
    myCurrencyCodeTable.CurrencyCode = myDataRecord.GetString(myDataRecord.GetOrdinal("CurrencyCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencySymbol")))
{
    myCurrencyCodeTable.CurrencySymbol = myDataRecord.GetString(myDataRecord.GetOrdinal("CurrencySymbol"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myCurrencyCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Rate")))
{
    myCurrencyCodeTable.Rate = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Rate"));
}

if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myCurrencyCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myCurrencyCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myCurrencyCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myCurrencyCodeTable;
}
}
